在 Go 中, 集合 集合本质上是同一类型项的组合,用于高效访问。"货舱" 代表我们希望将大量数据统一管理在一个标识符下,而不是使用多个独立变量。
1. 复合字面量
一个 复合字面量 是一种简洁的语法,用于用你期望的值初始化任意复合类型。它允许你通过如下语法,在一步中声明并初始化数组: type{value1, value2, ...}。
2. 零索引导航
数组的索引从 0 开始。一个包含 8 个行星的集合可以通过索引 0 到 7 来访问。访问超出此范围的索引会导致编译时错误或运行时恐慌。
图 16.1:索引为 0-7 的行星
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary purpose of a composite literal in Go?
To perform complex mathematical calculations on groups of numbers.
A concise syntax to declare and initialize a composite type with values.
To convert a slice into an array automatically.
To delete elements from a collection.
✅ Correct!
Correct! It allows you to define the structure and the data in one step.❌ Incorrect
Composite literals are for initialization, not calculation or deletion.QUESTION 2
If an array has 8 elements (as in Figure 16.1), what is the index of the first item?
1
-1
0
8
✅ Correct!
Correct. Go uses zero-based indexing for all collections.❌ Incorrect
Collections in Go always start at index 0.QUESTION 3
What does the ellipsis (...) in a function declaration typically indicate?
The function is incomplete.
The function accepts a variable number of arguments (variadic).
The function will loop infinitely.
The function returns a pointer.
✅ Correct!
Yes, the ellipsis denotes variadic parameters.❌ Incorrect
The ellipsis is used for variadic functions, allowing them to take any number of values.QUESTION 4
In the context of the Go Cargo Bay, what are 'collections'?
Variables that can only store numbers.
Just groups of things, typically of the same type.
Special functions that sort data.
Hardware components used for storage.
✅ Correct!
Exactly. Arrays, slices, and maps are all types of collections.❌ Incorrect
Collections are logical groupings of data within the code.QUESTION 5
What happens if you attempt to access planets[8] in an array of size 8?
It returns the first element again (wrap around).
It returns a null or empty string.
It causes a compile-time error or a runtime panic.
It automatically expands the array to size 9.
✅ Correct!
Correct! Array sizes are fixed and bounds are strictly enforced.❌ Incorrect
Arrays in Go are fixed-length; you cannot access an index equal to or greater than the length.Module Challenge: The Ship's Manifest
Representing physical items as digital arrays.
You are organizing a spaceship's inventory. You have a collection of personal items like stamps, books, and trophies. You need to initialize these in Go and perform operations on them.
Q
1. Arrays are for collecting many of the same type of thing. What collections from your own life (e.g., stamps, shoes) could you represent with an array? Explain your choice.
Solution:
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Q
2. Provide the syntax for a 'Step' function signature that takes two Universe types, intended for simulation operations.
Solution:
The signature is:
The signature is:
func Step(a, b Universe). This allows the function to operate on two collections, often used for double-buffering in simulations like the Game of Life.